package com.morgan.storageperlib.saf.handler

import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.documentfile.provider.DocumentFile

/**
 *description：创建文件夹，用户选择的文件夹下面 再创建 名称为 lastFolderName 的文件夹
 *<br>author：caowugao
 *<br>time：2021/3/5 0005 18:41
 */
open class CreateFolderHandler(lastFolderName : String?, act : AppCompatActivity, contract : ActivityResultContract<Intent, ActivityResult> = ActivityResultContracts.StartActivityForResult(), callback : (Boolean, DocumentFile?) -> Unit) : OpenFolderHandler(act, contract, { uri ->
    val docFile = DocumentFile.fromTreeUri(act, uri)
    try {
        var realDocFile = docFile!!
        if (lastFolderName != null) {
            realDocFile = realDocFile.createDirectory(lastFolderName)!!
        }
        callback.invoke(true, realDocFile)
    } catch (e : Throwable) {
        e.printStackTrace()
        callback.invoke(false, null)
    }
}){
}